package com.demo.apparel.mapper.web;

import com.demo.apparel.dto.web.AdminFeedbackDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AdminFeedbackMapper {

    @Select("""
        SELECT id, user_id, admin_id, content, status, created_time, updated_time, response, contact
        FROM feedback
        WHERE status = #{status}
        ORDER BY created_time DESC
        LIMIT #{offset}, #{limit}
        """)
    @Results(id = "AdminFeedbackDTOMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "admin_id", property = "adminId"),
            @Result(column = "content", property = "content"),
            @Result(column = "status", property = "status"),
            @Result(column = "created_time", property = "createdTime"),
            @Result(column = "updated_time", property = "updatedTime"),
            @Result(column = "response", property = "response"),
            @Result(column = "contact", property = "contact")
    })
    List<AdminFeedbackDTO> selectByStatus(@Param("status") Integer status,
                                          @Param("offset") int offset,
                                          @Param("limit") int limit);

    @Select("""
        SELECT id, user_id, admin_id, content, status, created_time, updated_time, response, contact
        FROM feedback
        ORDER BY created_time DESC
        LIMIT #{offset}, #{limit}
        """)
    @ResultMap("AdminFeedbackDTOMap") // ✅ 正确地复用上面的结果映射
    List<AdminFeedbackDTO> selectAll(@Param("offset") int offset,
                                     @Param("limit") int limit);

    @Select("SELECT COUNT(*) FROM feedback WHERE status = #{status}")
    long countByStatus(@Param("status") Integer status);

    @Select("SELECT COUNT(*) FROM feedback")
    long countAll();


    @Select("""
    SELECT id, user_id, admin_id, content, status, created_time, updated_time, response, contact
    FROM feedback
    WHERE status = #{status}
      AND (content LIKE CONCAT('%', #{contact}, '%') OR contact LIKE CONCAT('%', #{contact}, '%'))
    ORDER BY created_time DESC
    LIMIT #{offset}, #{limit}
""")
    @ResultMap("AdminFeedbackDTOMap")
    List<AdminFeedbackDTO> selectByStatusAndContact(@Param("status") Integer status,
                                                    @Param("contact") String contact,
                                                    @Param("offset") int offset,
                                                    @Param("limit") int limit);

    @Select("""
    SELECT COUNT(*)
    FROM feedback
    WHERE status = #{status}
      AND (content LIKE CONCAT('%', #{contact}, '%') OR contact LIKE CONCAT('%', #{contact}, '%'))
""")
    long countByStatusAndContact(@Param("status") Integer status, @Param("contact") String contact);

    @Select("""
    SELECT id, user_id, admin_id, content, status, created_time, updated_time, response, contact
    FROM feedback
    WHERE content LIKE CONCAT('%', #{contact}, '%') OR contact LIKE CONCAT('%', #{contact}, '%')
    ORDER BY created_time DESC
    LIMIT #{offset}, #{limit}
""")
    @ResultMap("AdminFeedbackDTOMap")
    List<AdminFeedbackDTO> selectByContact(@Param("contact") String contact,
                                           @Param("offset") int offset,
                                           @Param("limit") int limit);

    @Select("""
    SELECT COUNT(*)
    FROM feedback
    WHERE content LIKE CONCAT('%', #{contact}, '%') OR contact LIKE CONCAT('%', #{contact}, '%')
""")
    long countByContact(@Param("contact") String contact);


    /**
     *
     * @param feedbackId
     * @param adminId
     * @param response
     * @return
     */
    @Update("""
    UPDATE feedback 
    SET response = #{response}, 
        admin_id = #{adminId}, 
        status = #{status},
        updated_time = NOW()
    WHERE id = #{feedbackId}
""")
    int updateResponse(@Param("feedbackId") String feedbackId,
                       @Param("adminId") String adminId,
                       @Param("response") String response,
                       @Param("status") Integer status);



    @Delete("DELETE FROM feedback WHERE id = #{feedbackId}")
    int deleteById(@Param("feedbackId") String feedbackId);

    @Delete({
            "<script>",
            "DELETE FROM feedback WHERE id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    int deleteBatch(@Param("ids") List<String> ids);


}
